package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.LuziResponse;
import com.lottery.ruok.lottrey.ui.widget.HorizontalScrollView.ScrollRecyclerView;
import com.lottery.ruok.lottrey.ui.widget.HorizontalScrollView.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 作者：xuyf
 * <p>
 * 创建日期：2018/8/22$
 * <p>
 * 描述：路子列表Adapter
 */
public class LuziRecyclerViewAdapter extends RecyclerView.Adapter<LuziRecyclerViewAdapter.RecyclerHolder> {
    private List<LuziResponse> mData;
    private Context mContext;
    private int index = 0;
    private Map<Integer, View> mViewCache;
    private Map<Integer, View> mRecycleCache;
    private List<List<String>> all = new ArrayList<>();

    public LuziRecyclerViewAdapter(Context context, List<LuziResponse> data) {
        mContext = context;
        mData = data;
        mViewCache = new HashMap<>();
        mRecycleCache = new HashMap<>();
    }


    @Override
    public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.luzi_item_recyclerview, parent, false);
        RecyclerHolder recyclerHolder = new RecyclerHolder(view);
        return recyclerHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerHolder holder, int position) {
        LuziResponse data = mData.get(position);
        holder.mTextView.setText(data.getGameName());
        settingRecyclerView(position, holder.mRecyclerview);
        initData(holder.mRecyclerview, data.getData());
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }


    private void initData(ScrollRecyclerView view, List<String> data) {
        StringBuilder sb = new StringBuilder();
        for (String s : data) {
            sb.append(s);
        }
        while (fromatDataStep1(sb.toString().substring(index))) ;
        LuziItemAdapter luziItemAdapter = new LuziItemAdapter(mContext, fromatDataStep2());
        view.setAdapter(luziItemAdapter);
        view.smoothScrollToPosition(luziItemAdapter.getItemCount() - 1);
        index = 0;
        all.clear();
    }

    //数据转换
    public boolean fromatDataStep1(String json) {
        int x = 0;
        int a = 0;
        List<List> chao6 = new ArrayList<>();
        for (int i = 0; i < json.length(); i++) {
            if (json.charAt(0) != json.charAt(i)) {
                char[] chars = json.substring(0, i).toCharArray();
                chao6.add(new ArrayList());
                for (char c : chars) {
                    if (chao6.get(x).size() < 6) {
                        chao6.get(x).add(c + "");
                    } else {
                        x++;
                        List<String> newArr = new ArrayList();
                        newArr.add(c + "");
                        chao6.add(newArr);
                    }
                }
                index += i;
                for (List list : chao6) {
                    all.add(list);
                }
                break;
            } else {
                if (i == json.length() - 1) {
                    char[] chars = json.substring(0, json.length()).toCharArray();
                    chao6.add(new ArrayList());
                    for (char c : chars) {
                        if (chao6.get(a).size() < 6) {
                            chao6.get(a).add(c + "");
                        } else {
                            a++;
                            List<String> newArr = new ArrayList();
                            newArr.add(c + "");
                            chao6.add(newArr);
                        }
                    }
                    for (List list : chao6) {
                        all.add(list);
                    }
                    return false;
                }

            }
        }
        return true;
    }

    //每组数据如果未满6个就添加*，到后面的luziAdpater中处理
    private List<String> fromatDataStep2() {
        LinkedList<String> data = new LinkedList<>();
        for (List<String> list : all) {
            while (list.size() < 6)
                list.add("*");
        }
        for (List<String> list : all) {
            for (String s : list) {
                data.add(s);
            }
        }
        return data;
    }

    private void settingRecyclerView(int position, ScrollRecyclerView view) {
        //对每个luziRecycler只设置一次
        if (!mViewCache.containsKey(position)) {
            mViewCache.put(position, view);
//            view.setItemAnimator(new DefaultItemAnimator());
            // 设置布局管理器：瀑布流式
            StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(6,
                    StaggeredGridLayoutManager.HORIZONTAL);
            view.setLayoutManager(staggeredGridLayoutManager);
            // 根据需要设置间距等
            int right = (int) mContext.getResources().getDimension(R.dimen.dp_1);
            int bottom = (int) mContext.getResources().getDimension(R.dimen.dp_1);
            RecyclerView.ItemDecoration spacingInPixel = new SpaceItemDecoration(right, bottom);
            view.addItemDecoration(spacingInPixel);
        }
    }

    public class RecyclerHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        ScrollRecyclerView mRecyclerview;

        private RecyclerHolder(View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.tv_game_name);
            mRecyclerview = itemView.findViewById(R.id.recyclerview);
        }
    }
}